import io from 'socket.io-client';
console.log(io.socket); //undefine
// io('ws://localhost:4000');
// 默认情况下 io.socket并不存在  为了方便在这个Js文件中使用  把socket对象绑定到这里

export const socketInit = function (dispatch, userid) {
    if (!io.socket) {
        io.socket = io('ws://localhost:4000');

        io.socket.on('receiveMessage', function (chatMsg) {
            console.log("client receive", chatMsg);
            if (userid == chatMsg.from || userid == chatMsg.to) {
                dispatch({type: "Receive_OneMsg",data: {chatMsg, userid}});
            }
        });
    }
}

export const sendMsg = function (param) {
    if(io.socket){
        io.socket.emit("sendMessage", param)
    }else{
        console.error("socket is null")
    }
}